package cn.cjlmonster.demo02.ui;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import java.util.ArrayList;
import java.util.List;

import cn.cjlmonster.demo02.R;
import cn.cjlmonster.demo02.base.BaseAppCompatActivity;

/**
 * This project is Demo02
 * Created by cjl on 2021/12/5 1:33 上午.
 */
public class ViewPager2Activity extends BaseAppCompatActivity {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_view_pager2;
    }

    @Override
    protected boolean canBack() {
        return true;
    }

    @Override
    protected void initView() {
        ViewPager2 viewPager2 = findViewById(R.id.viewPager2);

        List<String> mData = new ArrayList<>();
        mData.add("测试1");
        mData.add("测试2");
        mData.add("测试3");
        PagerAdapter adapter = new PagerAdapter(this, mData);
        viewPager2.setAdapter(adapter);
    }

    static class PagerAdapter extends RecyclerView.Adapter<PagerAdapter.Holder> {

        private final LayoutInflater inflater;
        private final List<String> mData;

        private static final int[] colors = {
                Color.BLUE,
                Color.YELLOW,
                Color.GREEN
        };

        public PagerAdapter(Context context, List<String> mData) {
            this.inflater = LayoutInflater.from(context);
            this.mData = mData;
        }

        @NonNull
        @Override
        public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = inflater.inflate(R.layout.item_page, parent, false);
            return new Holder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull Holder holder, int position) {
              holder.tv_msg.setText(mData.get(position));
              holder.itemView.setBackgroundColor(colors[position % colors.length]);
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        static class Holder extends RecyclerView.ViewHolder {

            TextView tv_msg;

            public Holder(@NonNull View itemView) {
                super(itemView);
                tv_msg = itemView.findViewById(R.id.tv_msg);
            }
        }
    }
}
